<?php
/**
 * Created by PhpStorm.
 * User: xionggang
 * Date: 15-4-28
 * Time: 下午3:43
 */
class Article_model extends MY_Model {

    public function __construct() {
        $this->_table = 'db_article';
        parent::__construct();
    }

    /**
     *   按条件获取制定条件文章数量--正常状态文章
     *
     */
    public function get_count($typeid=0,$write='', $title='',$typeid='',$is_del=0,$time_start='',$time_end='',$tag='')
    {

        $typeid && $this->db->where('typeid',$typeid);
        $write   && $this->db->where('writer',$write);
        $typeid  && $this->db->where('typeid',$typeid);
        $this->db->where('is_del',$is_del);
        $time_start  && $this->db->where('pubtime >',$time_start);
        $time_end  && $this->db->where('pubtime <',$time_end);
        $title   && $this->db->like('title', $title);
        $tag   && $this->db->like('tag', $tag);
        $this->db->from($this->_table);

        return $this->db->count_all_results();
    }

    /**
     *  获取分页查询数据--正常状态文章
     *
     */
    public function get_page($typeid=0,$write='', $title='',$typeid='',$is_del=0,$time_start='',$time_end='',$tag='',$limit=0, $offset=0)
    {
        $typeid && $this->db->where('typeid',$typeid);
        $write   && $this->db->where('writer',$write);
        $typeid  && $this->db->where('typeid',$typeid);
        $this->db->where('is_del',$is_del);
        $time_start  && $this->db->where('pubtime >',$time_start);
        $time_end  && $this->db->where('pubtime <',$time_end);
        $title   && $this->db->like('title', $title);
        $tag   && $this->db->like('tag', $tag);
        $this->db->order_by('id', 'desc');
        $limit && $this->db->limit($limit, $offset);
        $query = $this->db->get($this->_table);
        if ($query->num_rows() > 0) {
            return $query->result_array();
        } else {
            return false;
        }
    }

    /**
     * auto:xg
     * 将文章放入回收站或还原（也就是修改is_del字段的值，0为正常文章。1为回收站文章）
     */
    public function set_is_del($id,$flag){
        $set_date = array('is_del'=>$flag);
        $this->db->where('id', $id);
        $this->db->update($this->_table, $set_date);
        return $this->db->affected_rows();
    }

    /**
     * auto:xg
     * 批量修改文章的标签，包括flag字段和mark字段
     */
    public function add_flag_mark($id,$fl,$ziduan){
        $query = $this->db->select($ziduan)->get_where($this->_table, array('id' => $id));

        $res = $query->row_array();
        $flag_old = $res[$ziduan];

        if (isset($flag_old) && !empty($flag_old)) {
            $cunzai = strpos( ','.$flag_old, $fl);
        } else {
            $cunzai = '';
        }

        if (isset($cunzai) && !empty($cunzai)) {
            return 1;
        } else {
            $flag_new = $flag_old . ',' . $fl;
            $flag_new =trim($flag_new,',');
            $flag_arr = array($ziduan => $flag_new);
            $this->db->where('id', $id);
            $this->db->update($this->_table, $flag_arr);
        }
    }

    /**
     * auto:xg
     * 批量删除标签，包括flag字段和mark字段
     */
    public function del_flag_mark($id,$fl,$ziduan){
        $query = $this->db->select($ziduan)->get_where($this->_table, array('id' => $id));

        $res = $query->row_array();
        $flag_old = $res[$ziduan];

        if (isset($flag_old) && !empty($flag_old)) {
            $cunzai = strpos( ','.$flag_old, $fl);
        } else {
            $cunzai = '';
        }

        if (isset($cunzai) && !empty($cunzai)) {
            $flag_new = $flag_old . ',';
            $flag_new = str_replace($fl.',','',$flag_new);
            $flag_new =trim($flag_new,',');
            $flag_arr = array($ziduan => $flag_new);
            $this->db->where('id', $id);
            $this->db->update($this->_table, $flag_arr);
        } else {
            return 1;
        }
    }

    /**
     * auto:xg
     * 获取单个文章信息
     */
    public function get_one_article($id){
        $query = $this->db->get_where($this->_table, array('id' => $id));
        $res = $query->row_array();
        return $res;
    }

    /**
     * auto:xg
     * 保存文章信息
     */
    public function save_article($id='',$date){
        if(empty($id)){
            $this->db->insert($this->_table, $date);
        }else{
            $this->db->where('id', $id);
            $this->db->update($this->_table, $date);
        }
        return $this->db->affected_rows();
    }
}